package com.duoduo.visual;

import java.util.Arrays;

import com.duoduo.algs4.util.Util;

import edu.princeton.cs.introcs.Draw;
import edu.princeton.cs.introcs.StdRandom;

public class DrawArray {

	public static Draw DrawArrayUseBar(int[] a, String name) {
		double[] b = Util.copy(a);
		return DrawArrayUseBar(b, name);
	}

	public static Draw DrawArrayUseBar(double[] a, String name) {
		Draw draw = new Draw(name);
		return drawArrayUseBar(a, 0.5, 2.0, draw);
	}

	public static Draw DrawArrayUseBar(int[] a, double X_DOWN, double Y_DOWN,
			String name) {
		Draw draw = new Draw(name);
		return DrawArrayUseBar(a, X_DOWN, Y_DOWN, draw);
	}

	public static Draw DrawArrayUseBar(int[] a, double X_DOWN, double Y_DOWN,
			Draw draw) {
		double[] b = Util.copy(a);
		return drawArrayUseBar(b, X_DOWN, Y_DOWN, draw);
	}

	public static Draw DrawArrayUseBar(double[] a, double X_DOWN,
			double Y_DOWN, String name) {
		Draw draw = new Draw(name);
		return drawArrayUseBar(a, X_DOWN, Y_DOWN, draw);
	}

	public static Draw drawArrayUseBar(double[] a, double X_DOWN,
			double Y_DOWN, Draw draw) {
		// 建议Y_DOWN为最大数值的两倍
		int N = a.length;
		for (int i = 0; i < N; i++) {
			double x = 1.0 * i / N;
			double y = a[i] / Y_DOWN;
			double rw = X_DOWN / N;
			double rh = a[i] / Y_DOWN;
			draw.filledRectangle(x, y, rw, rh);

		}
		return draw;
	}
	public static Draw DrawArrayUsePoints(double[] a,double Y_MAX, String name) {
		int X_MAX = a.length;
		Draw draw = new Draw(name);
		draw.setXscale(0, X_MAX);
		draw.setYscale(0, Y_MAX);
		draw.setPenRadius(.01);
		for (int i = 0; i < X_MAX; i++) {
			draw.point(i, a[i]);
		}
		return draw;
	}


	public static void main(String[] args) {
		// Draw DrawRandomArray()
		int N = 50;
		double[] a = new double[N];
		for (int i = 0; i < N; i++) {
			a[i] = StdRandom.random();
		}
		Arrays.sort(a);
		DrawArrayUseBar(a, "排序数组");

		// Draw DrawSortedArray2()
		N = 50;
		int[] b = new int[N];
		for (int i = 0; i < N; i++) {
			b[i] = i;
		}
		Arrays.sort(b);

		DrawArrayUseBar(b, 0.5, N * 2, "设定高度的排序数组");
		// DrawLine();

		N = 100;
		Draw draw = new Draw("画线");
		draw.setXscale(0, N);
		draw.setYscale(0, N * N);
		draw.setPenRadius(.01);
		for (int i = 0; i < N; i++) {
			draw.point(i, i);
			draw.point(i, i * i);
			draw.point(i, i * Math.log(i));
		}

		// Draw DrawRandomArray()
		a = new double[N];
		for (int i = 0; i < N; i++) {
			a[i] = StdRandom.random();
		}
		DrawArrayUseBar(a, "随机数组");
		// drawArrayUsePoints();
		a = new double[N];
		for (int i = 0; i < N; i++) {
			a[i] = i;
		}
		DrawArrayUsePoints(a,N,"点线图");
	}

}
